using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;
using System.Threading;

namespace PokerLearner.Poker
{
	public class ExporterMatLab : IExporter
	{
		#region IExporter Members

		public void Export(string name)
		{
			ExportData(name);
		}

		#endregion

		#region Helper Methods

		private object[] GetPositionOutput(Position position)
		{
			object[] ret = new object[] { -1, -1, -1 };

			if (position == Position.SmallBlind)
				ret[0] = 1;
			else if (position == Position.BigBlind)
				ret[1] = 1;
			else if (position == Position.Dealer)
				ret[2] = 1;

			return ret;
		}

		private object[] GetPlayOutput(Play play)
		{
			object[] ret = new object[] { -1, -1, -1, -1, -1, -1 };

			if (play.Type == PlayType.Check)
				ret[0] = 1;
			else if (play.Type == PlayType.Call)
				ret[1] = 1;
			else if (play.Type == PlayType.Bet)
				ret[2] = 1;
			else if (play.Type == PlayType.Raise)
				ret[3] = 1;
			else if (play.Type == PlayType.AllIn)
				ret[4] = 1;
			else if (play.Type == PlayType.Fold)
				ret[5] = 1;

			return ret;
		}

		#endregion

		#region Export Methods

		public void ExportData(string name)
		{
			FileStream dataStream = new FileStream(name + ".mdat", FileMode.Create);
			StreamWriter writer = new StreamWriter(dataStream);

			NumberFormatInfo format = new NumberFormatInfo();
			format.CurrencyDecimalSeparator = ".";

			foreach (Record record in PokerLogger.Instance.PermanentRecords)
			{
				// Write identifier, position and preflop hand
				writer.Write("{0},", record.Identifier);
				writer.Write("{0},{1},{2},", GetPositionOutput(record.Position));
				writer.Write("{0},", record.NumPlayers);

				writer.Write("{0},", record.PreFlopHand.Value);
				if (record.PreFlopPlays != null)
				{
					writer.Write("{0},", record.PreFlopPlays.Count);
					writer.Write("{0},{1},{2},{3},{4},{5},", GetPlayOutput(record.PreFlopPlays[record.PreFlopPlays.Count - 1]));
					writer.Write("{0},", Record.FindTotalPlayValue(record.PreFlopPlays));
				}
				else
					writer.Write("-1,-1,-1,-1,-1,-1,-1,-1,");

				writer.Write("{0},", record.NumPlayersFlop);

				if (record.FlopHand != null)
					writer.Write("{0},{1},", record.FlopHand.Value, record.FlopCommonHand.Value);
				else
					writer.Write("-1,-1,");

				if (record.FlopPlays != null)
				{
					writer.Write("{0},", record.FlopPlays.Count);
					writer.Write("{0},{1},{2},{3},{4},{5},", GetPlayOutput(record.FlopPlays[record.FlopPlays.Count - 1]));
					writer.Write("{0},", Record.FindTotalPlayValue(record.FlopPlays));
				}
				else
					writer.Write("-1,-1,-1,-1,-1,-1,-1,-1,");

				writer.Write("{0},", record.NumPlayersTurn);

				if (record.TurnHand != null)
					writer.Write("{0},{1},", record.TurnHand.Value, record.TurnCommonHand.Value);
				else
					writer.Write("-1,-1,");

				if (record.TurnPlays != null)
				{
					writer.Write("{0},", record.TurnPlays.Count);
					writer.Write("{0},{1},{2},{3},{4},{5},", GetPlayOutput(record.TurnPlays[record.TurnPlays.Count - 1]));
					writer.Write("{0},", Record.FindTotalPlayValue(record.TurnPlays));
				}
				else
					writer.Write("-1,-1,-1,-1,-1,-1,-1,-1,");

				writer.Write("{0},", record.NumPlayersRiver);

				if (record.RiverHand != null)
					writer.Write("{0},{1},", record.RiverHand.Value, record.RiverCommonHand.Value);
				else
					writer.Write("-1,-1,");

				if (record.RiverPlays != null)
				{
					writer.Write("{0},", record.RiverPlays.Count);
					writer.Write("{0},{1},{2},{3},{4},{5},", GetPlayOutput(record.RiverPlays[record.RiverPlays.Count - 1]));
					writer.Write("{0},", Record.FindTotalPlayValue(record.RiverPlays));
				}
				else
					writer.Write("-1,-1,-1,-1,-1,-1,-1,-1,");

				writer.WriteLine(String.Format(format, "{0}", ((record.Win) ? 0.9 : -0.9)));
			}
			writer.Close();
		}

		#endregion
	}
}
